/*
 * Copyright (c) 2003-2004, Inversoft, All Rights Reserved
 */
package org.emergent.antbite.savant.ant;


import java.io.File;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Ant;

import org.emergent.antbite.savant.LocalProject;
import org.emergent.antbite.savant.LocalProjectBuilder;


/**
 * <p>
 * This class is the local project builder that uses the ant
 * framework to call out to another ant build file.
 * </p>
 *
 * @author Brian Pontarelli
 */
public class AntLocalProjectBuilder implements LocalProjectBuilder {

    private Project antProject;


    public AntLocalProjectBuilder(Project antProject) {
        this.antProject = antProject;
    }


    /**
     * Builds the given LocalProject.
     */
    public void build(LocalProject project) {
        // Test if the project exists and if not, bail
        File test = new File(project.getDir(), project.getAntfile());
        if (!test.exists()) {
            return;
        }

        Ant ant = (Ant) antProject.createTask("ant");
        ant.setAntfile(project.getAntfile());
        ant.setDir(project.getDir());
        ant.setInheritAll(false);
        ant.setInheritRefs(false);

        if (project.getTarget() != null) {
            ant.setTarget(project.getTarget());
        }

        ant.perform();
    }
}
